package com.angel.core.util;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: StringUtils
 * @Description: 字符处理
 * @author Liucx
 * @date 2014-4-24 下午4:25:44
 * 
 * @tag v1.0
 */
public class StringUtils {

	private static String EMPTY = "";

	private StringUtils() {
	}

	public static boolean notNull(Object str) {
		return null != str && !"".equals(str);
	}

	public static boolean isEmpty(String str) {
		return null == str || str.trim().length() == 0;
	}

	public static String getRandomStr(int n) {
		Random random = new Random();
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < n; i++) {
			str.append(random.nextInt(10));
		}
		return str.toString();
	}

	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public static String replaceEnter(String str) {
		if (str == null)
			return null;
		return str.replaceAll("\r", "").replaceAll("\n", "");
	}

	/**
	 * 字符转换
	 * 
	 * @param str
	 * @return
	 */
	public static String toUTF8(String str) {
		if (str == null || str.equals("")) {
			return "";
		}
		try {
			return new String(str.getBytes("ISO8859-1"), "UTF-8");
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}

	public static String to(String str, String charset) {
		if (str == null || str.equals("")) {
			return "";
		}
		try {
			return new String(str.getBytes("ISO8859-1"), charset);
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}

	/**
	 * 去除HTML 元素
	 * 
	 * @param element
	 * @return
	 */
	public static String getTxtWithoutHTMLElement(String element) {
		if (null == element) {
			return element;
		}
		Pattern pattern = Pattern.compile("<[^<|^>]*>");
		Matcher matcher = pattern.matcher(element);
		StringBuffer txt = new StringBuffer();
		while (matcher.find()) {
			String group = matcher.group();
			if (group.matches("<[\\s]*>")) {
				matcher.appendReplacement(txt, group);
			} else {
				matcher.appendReplacement(txt, "");
			}
		}
		matcher.appendTail(txt);
		String temp = txt.toString().replaceAll("[\r|\n]", "");
		// 多个连续空格替换为一个空格
		temp = temp.replaceAll("\\s+", " ");
		return temp;
	}

	/**
	 * obj转为String
	 * 
	 */
	public static String transNullValue(Object obj) {
		return obj != null ? obj.toString().trim() : "";
	}

	/**
	 * 科学计数 转为普通计数 String类型 1.23456789E8 转换为123456789
	 * 
	 */
	public static String science2String(String scien) {
		return new BigDecimal(scien).toPlainString();
	}

	/**
	 * 字符串是否全为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNum(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		return isNum.matches();
	}

	public static String toString(Object obj) {
		return obj == null ? "" : obj.toString();
	}

	public static String join(Collection<? extends Object> connection,
			String separator, String fields) {
		Iterator<? extends Object> iterator = connection.iterator();
		return join(iterator, separator, fields);
	}

	public static String join(Collection<? extends Object> connection,
			String separator) {
		return join(connection.iterator(), separator);
	}

	private static String join(Iterator<? extends Object> iterator,
			String separator, String... fieldNames) {
		// handle null, zero and one elements before building a buffer
		if (iterator == null) {
			return null;
		}
		if (!iterator.hasNext()) {
			return EMPTY;
		}

		// two or more elements
		StringBuffer buf = new StringBuffer(256); // Java default is 16,
		while (iterator.hasNext()) {
			Object obj = iterator.next();
			if (obj != null) {
				if (fieldNames != null && fieldNames.length > 0) {
					try {
						Field field = null;
						Object value = null;
						Class<? extends Object> cls = obj.getClass();
						for (String fieldName : fieldNames) {
							field = cls.getDeclaredField(fieldName);
							field.setAccessible(true);
							value = field.get(obj);
							buf.append(value);
						}
					} catch (NoSuchFieldException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				} else {
					buf.append(obj);
				}
			}
			if (separator != null) {
				buf.append(separator);
			}
		}
		return org.apache.commons.lang.StringUtils.substringBeforeLast(buf.toString(), ",");
	}
}